//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shp.Runtime.Sitka.ServiceGateway.SitkaClient;
using Shp.Runtime.Sitka.ServiceAgent.Managers;
using Shp.Runtime.Sitka.ServiceAgent;
using System.Globalization;
using LitwareHR.Recruiting.DataModel.Sitka.Constants;
using LitwareHR.Recruiting.Contract;
using System.Collections.ObjectModel;

namespace LitwareHR.Recruiting.DataModel.Sitka.Tests.Mocks
{
    public class DataInitialize
    {
        public string Container { get; set; }
        public string Metadata { get; set; }

        public DataInitialize(string container, string metadata)
        {
            Container = container;
            Metadata = metadata;
        }

        private void InsertEntity(Entity entity)
        {
            DefaultTenantManager tm = new DefaultTenantManager();

            Scope scope = new Scope();
            scope.AuthorityId = tm.GetAuthorityId(Container);
            scope.ContainerId = Container;

            ISitkaSoapService stratusProxy = SitkaProxyFactory.BuildProxy();

            stratusProxy.Create(scope, entity);
        }

        private void InsertMetadataEntity(Entity entity)
        {
            DefaultTenantManager tm = new DefaultTenantManager();

            Scope scope = new Scope();
            scope.AuthorityId = tm.GetAuthorityId(Metadata);
            scope.ContainerId = Metadata;

            ISitkaSoapService stratusProxy = SitkaProxyFactory.BuildProxy();

            stratusProxy.Create(scope, entity);
        }

        public void CreateResumeInstance(Guid id, string additionalInfo, string address, string applicantName, string applicantSurname, string phoneNumber, DateTime date, Guid positionId, string upn, bool isVideoAvailable, bool isVideoUploaded, Collection<Field> fields)
        {
            Entity entity = createResumeInstance(ref id, additionalInfo, address, applicantName, applicantSurname, phoneNumber, date, ref positionId, upn, isVideoAvailable, isVideoUploaded); 
            
            foreach (Field field in fields)
            {
                entity.Properties.Add(field.Name, field.Value);
            }
            
            InsertEntity(entity);
        }

        public void CreateResumeInstance(Guid id, string additionalInfo, string address, string applicantName, string applicantSurname, string phoneNumber, DateTime date, Guid positionId, string upn, bool isVideoAvailable, bool isVideoUploaded)
        {
            Entity entity = createResumeInstance(ref id, additionalInfo, address, applicantName, applicantSurname, phoneNumber, date, ref positionId, upn, isVideoAvailable, isVideoUploaded);
            InsertEntity(entity);
        }

        private static Entity createResumeInstance(ref Guid id, string additionalInfo, string address, string applicantName, string applicantSurname, string phoneNumber, DateTime date, ref Guid positionId, string upn, bool isVideoAvailable, bool isVideoUploaded)
        {
            Entity entity = EntityManager.CreateEntity("Resume", id.ToString());
            entity.Properties.Add(ResumeEntityFields.AdditionalInfo, additionalInfo);
            entity.Properties.Add(ResumeEntityFields.Address, address);
            entity.Properties.Add(ResumeEntityFields.ApplicantName, applicantName);
            entity.Properties.Add(ResumeEntityFields.ApplicantSurname, applicantSurname);
            entity.Properties.Add(ResumeEntityFields.PhoneNumber, phoneNumber);
            entity.Properties.Add(ResumeEntityFields.Date, date);
            entity.Properties.Add(ResumeEntityFields.PositionId, positionId.ToString());
            entity.Properties.Add(ResumeEntityFields.Upn, upn);
            entity.Properties.Add(ResumeEntityFields.IsVideoAvailable, isVideoAvailable);
            entity.Properties.Add(ResumeEntityFields.IsVideoUploaded, isVideoUploaded);
            return entity;
        }

        public void AddTenant(string tenantId, string tenantAlias, string silverligthId, string silverligthKey)
        {
            Entity entity = EntityManager.CreateEntity("Tenant", tenantId);
            entity.Properties.Add("Name", "name");
            entity.Properties.Add("Alias", tenantAlias.ToLower());
            if (silverligthId != null)
            {
                entity.Properties.Add("SilverlightAccountId", silverligthId);
            }
            if (silverligthKey != null)
            {
                entity.Properties.Add("SilverlightAccountKey", silverligthKey);
            }
            entity.Properties.Add("Style", Guid.NewGuid().ToString());
            entity.Properties.Add("Layout", (0).ToString(CultureInfo.InvariantCulture));

            InsertMetadataEntity(entity);
        }

        public void CreateEntityDef(Guid id, string entityKind)
        {
            Entity entity = EntityManager.CreateEntity("EntityDef", id.ToString());
            entity.Properties.Add("Name", entityKind);

            InsertMetadataEntity(entity);
        }

        public void AddEntityDefField(Guid tenantId,Guid entityDefId, string name, bool isVisibleFromApplicationForm)
        {
            Entity entity = EntityManager.CreateEntity("EntityDefField", Guid.NewGuid().ToString());
            entity.Properties.Add("Name", name);
            entity.Properties.Add("TypeId", "b44a04ca-3426-4c8d-9fc0-431de22d6f4e");
            entity.Properties.Add("IsShared", false);
            entity.Properties.Add("EntityDefId", entityDefId.ToString());
            entity.Properties.Add("TenantId", tenantId.ToString());
            entity.Properties.Add("IsVisibleFromApplicationForm", isVisibleFromApplicationForm);

            InsertMetadataEntity(entity);
        }
    }
}
